package com.zy.experience_system.service.impl;

import com.zy.experience_system.entity.*;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.TeachForm;
import com.zy.experience_system.repository.TeachInformationRepository;
import com.zy.experience_system.repository.TestSetRepository;
import com.zy.experience_system.service.TeachService;
import com.zy.experience_system.util.MyDateUtil;
import com.zy.experience_system.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeachServiceImpl implements TeachService {

    @Autowired
    private TeachInformationRepository teachInformationRepository;
    @Autowired
    private TestSetRepository testSetRepository;

    @Override
    public void save(TeachForm teachForm) {
        TeachInformation teachInformation;
        if (teachForm.getId() == null) {
            teachInformation = new TeachInformation();
        } else {
            teachInformation = teachInformationRepository.findById(teachForm.getId()).get();
        }
        BeanUtils.copyProperties(teachForm, teachInformation);
        teachInformationRepository.save(teachInformation);
    }

    @Override
    public List<TeachInformation> findAll() {
        List<TeachInformation> teachInformationList = teachInformationRepository.findAll();
        return teachInformationList;
    }

    @Override
    public TeachDataVO findTeachDataVO(String teachId) {
        TeachDataVO teachDataVO = new TeachDataVO();
        TeachInformation teachInformation = teachInformationRepository.findByTeachId(teachId);
        TeachInfoVO teachInfoVO = new TeachInfoVO();
        BeanUtils.copyProperties(teachInformation, teachInfoVO);
        teachDataVO.setTeachInfoVO(teachInfoVO);
        List<TestSet> testSetList = testSetRepository.findAllByTeachId(teachId);
        List<StuSetVO> stuSetVOList = new ArrayList<>();
        for (TestSet testSet : testSetList) {
            StuSetVO stuSetVO = new StuSetVO();
            stuSetVO.setSetId(testSet.getSetId());
            stuSetVO.setSetName(testSet.getSetName());
            stuSetVO.setTeachId(testSet.getTeachId());
            stuSetVO.setTeachName(testSet.getTeachName());
            stuSetVO.setStartTime(MyDateUtil.date2String(testSet.getStartTime()));
            stuSetVO.setOverTime(MyDateUtil.date2String(testSet.getOverTime()));
            stuSetVOList.add(stuSetVO);
        }
        teachDataVO.setStuSetVOList(stuSetVOList);
        return teachDataVO;
    }
}
